package com.study.talk.ui.app.group

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.study.talk.ui.app.base.BaseViewModel
import com.study.talk.data.model.GroupInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AddGroupViewModel @Inject constructor(application: Application) : BaseViewModel(application) {

    private val _groups = MutableLiveData<List<GroupInfo>>()
    val groups: LiveData<List<GroupInfo>> get() = _groups

    fun loadGroups(keyword: String) {
        viewModelScope.launch {
            groupRepository.searchGroups(keyword, success = {
                if (it.isEmpty()) {
                    showToast("暂无符合条件的群聊...")
                } else {
                    _groups.postValue(it)
                }
            }, fail = {
                showToast(it)
            })

        }
    }
}